package com.xh.petdogexpert.user.service.imp;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xh.petdogexpert.base.BaseException;
import com.xh.petdogexpert.base.ValudateForm;
import com.xh.petdogexpert.user.dto.Member;
import com.xh.petdogexpert.user.mapper.MemberMapper;
import com.xh.petdogexpert.user.service.IMemberService;

/**
 * @author xin
 *
 */
@Transactional
@Service
public class MemberServiceImp implements IMemberService {

	@Autowired
	MemberMapper memberMapper;

	@Autowired
	ValudateForm valudateForm;

	private Logger logger = LoggerFactory.getLogger(MemberServiceImp.class);

	/**
	 * 更新会员的额外信息
	 * 
	 * @author xin
	 * @data 2016年2月20日
	 */
	@Override
	public int updateMember(Member member) throws BaseException {

		if (member == null || member.getUid() == null) {
			throw new BaseException("出错, 无法获取用户ID");
		}
		try {
			return memberMapper.updateMember(member);
		} catch (Exception e) {
			e.printStackTrace();
			throw new BaseException("更新会员信息出错");
		}
	}

	/**
	 * 获取会员信息，如果数据库没有则插入一条
	 * 
	 * @author xin
	 * @data 2016年2月20日
	 */
	@Override
	public Member getMember(Integer oid) throws BaseException {

		Member member = new Member();
		member.setUid(oid);
		List<Member> members = memberMapper.queryMembers(member);
		if (members.size() > 1) {
			throw new BaseException("获取多于一个会员的数据");
		} else if (members == null || members.size() == 0) {

			memberMapper.addMember(member);
			return member;

		} else {
			return members.get(0);
		}

	}

}
